<template>
    <div class="xtx-goods-page">
        <div class="container">
            <!-- 判断 :  -->
            <div v-if="info.categories">
                <!-- 面包屑 -->
                <XtxBread>
                    <XtxBreadItem to="/">首页</XtxBreadItem>
                    <XtxBreadItem :to="`/category/${info.categories[1].id}`">{{ info.categories[1].name }}</XtxBreadItem>
                    <XtxBreadItem :to="`/category/sub/${info.categories[0].id}`">{{ info.categories[0].name }}</XtxBreadItem>
                    <XtxBreadItem to="/">{{info.name}}</XtxBreadItem>
                </XtxBread>
                <!-- 商品信息 -->
                <div class="goods-info">
                    <div class="media">
                        <GoodsImage :images="info.mainPictures" />
                        <GoodsSale :goods="info" />
                    </div>
                    <div class="spec">
                        <!-- 详情 -->
                        <GoodsInfo :goods="info" />
                        <!-- 规格组件 -->
                        <GoodsSku :goods="info" skuId="1369155864430120962" @changeSku="changeSku"/>
                        <!-- 数量选择 -->
                        <XtxNumbox v-model="count" />
                        <!-- 按钮 -->
                        <XtxButton type="primary" style="margin-top:20px;" @click="addCart"> 加入购物车 </XtxButton>
                    </div>
                </div>
                <!-- 商品详情 -->
                <div class="goods-footer">
                    <div class="goods-article">
                        <!-- 商品+评价 -->
                        <div class="goods-tabs">
                            <GoodsDetail :goods="info" />
                        </div>
                    </div>
                    <!-- 热榜+专题推荐 -->
                    <div class="goods-aside">
                        <GoodsHot :type="1" />
                        <GoodsHot :type="2" />
                        <GoodsHot :type="3" />
                    </div>
                </div>
            </div>
            <!-- 占位 -->
            <div v-else>
                 <!-- 面包屑 -->
                <XtxBread>
                    <XtxBreadItem to="/">首页</XtxBreadItem>
                </XtxBread>
                <!-- 商品信息 -->
                <div class="goods-info"></div>
                <!-- 商品详情 -->
                <div class="goods-footer">
                    <div class="goods-article">
                        <!-- 商品+评价 -->
                        <div class="goods-tabs"></div>
                    </div>
                    <!-- 24热榜+专题推荐 -->
                    <div class="goods-aside"></div>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup name="Goods">
    import useStore from '@/store'
    import { storeToRefs } from 'pinia'
    import { ref,watchEffect } from 'vue'
    import { useRoute } from 'vue-router'
    import GoodsImage from './components/goods-image.vue'
    import GoodsInfo from './components/goods-info.vue'
    import GoodsSale from './components/goods-sale.vue'
    import GoodsSku from './components/goods-sku.vue'
    import GoodsDetail from './components/goods-detail.vue'
    import GoodsHot from './components/goods-hot.vue'
    import Message from '@/components/message'
import { CartItem } from '@/types/cart'
    const { goods,cart } = useStore()
    const route = useRoute()
    // 监听路由变化 发送请求
    watchEffect(() => {
        const id = route.params.id as string
        if (route.fullPath === `/goods/${id}`) {
            goods.resetGoodsInfo()
            goods.getGoodsInfo(id)
        }
    })
    const { info } = storeToRefs(goods)
    // 自定义事件
    const changeSku = (skuId: string) => {
        // 根据接收到的 skuId 找到对应的 sku
        currentSkuId.value = skuId
        const sku = info.value.skus.find((item) => item.id === skuId)
        if (sku) {
            info.value.inventory = sku.inventory
            info.value.price = sku.price
            info.value.oldPrice = sku.oldPrice
        }
    }
    const count = ref(1)
    const currentSkuId = ref('')
    // 加入购物车
    const addCart = async () => {
        // 未选择
        if(!currentSkuId.value) return Message.warning('请选择完整信息')
        const sku = info.value.skus.find((item) => item.id === currentSkuId.value)!
        const attrsText = sku.specs.map((item) => item.name + ':' + item.valueName).join(' ')
        await cart.addCart({
            skuId: currentSkuId.value, //当前skuId
            count: count.value, //数量
            id: info.value.id, //商品id
            name: info.value.name, //当前商品名字
            picture: info.value.mainPictures[0], //图片
            price: info.value.price, //价钱
            attrsText,//规格和选中具体规格的结合
            selected: true, //选中状态
            nowPrice: info.value.price, //价钱
            stock: info.value.inventory, //库存
            isEffective: true, //有消息
        }as CartItem)
        Message.success('加入购物车成功')
    }
</script>

<style scoped lang="less">
    .goods-info {
        min-height: 600px;
        background: #fff;
        display: flex;
        .media {
            width: 580px;
            height: 600px;
            padding: 30px 50px;
        }
        .spec {
            flex: 1;
            padding: 30px 30px 30px 0;
        }
    }
    .goods-footer {
        display: flex;
        margin-top: 20px;
        .goods-article {
            width: 940px;
            margin-right: 20px;
        }
        .goods-aside {
            width: 280px;
            min-height: 1000px;
        }
    }
    .goods-tabs {
        min-height: 600px;
        background: #fff;
    }
    .goods-warn {
        min-height: 600px;
        background: #fff;
        margin-top: 20px;
    }
</style>